//
//  ApplicationSetting.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/3/27.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "ApplicationSetting.h"
#import <CYLTabBarController.h>
#import "B_LoginFirstViewController.h"
#import "GSKeyChainDataManager.h"

#define FONT_SCALE_OF_IPHONE6P 1.12


@implementation ApplicationSetting

static ApplicationSetting *manager;

+ (ApplicationSetting *)defaultApplicationSetting {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[ApplicationSetting alloc] init];
    });
    return manager;
}

+ (NSDictionary *)getApplicationSetting {
    NSString *settingPath = [[NSBundle mainBundle] pathForResource:@"ApplicationSetting" ofType:@"plist"];
    NSDictionary *settingDictionary = [[NSDictionary alloc] initWithContentsOfFile:settingPath];
    NSLog(@"settingDictionary is %@", settingDictionary);
    return settingDictionary;
}

+ (NSArray *)getTabbarInfo {
    NSString *settingPath = [[NSBundle mainBundle] pathForResource:@"TabbarInfo" ofType:@"plist"];
    NSDictionary *settingDictionary = [[NSDictionary alloc] initWithContentsOfFile:settingPath];
    return [settingDictionary objectForKey:@"Item"];
}

CGFloat getLittleFont() {
    return GETLITTLEFONT() * (GETSCREENWIDHT() > 375 ? FONT_SCALE_OF_IPHONE6P : 1);
}

CGFloat getMiddleFont() {
    return GETMIDDLEFONT() * (GETSCREENWIDHT() > 375 ? FONT_SCALE_OF_IPHONE6P : 1);
}

CGFloat getLargeFont() {
    return GETLARGEFONT() * (GETSCREENWIDHT() > 375 ? FONT_SCALE_OF_IPHONE6P : 1);
}


NSString * getShowView() {
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"ShowView"]) {
        
    } else {
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"ShowView"];
    }
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"ShowView"];
}
void setShowView(NSString * str) {
    [[NSUserDefaults standardUserDefaults] setObject:str forKey:@"ShowView"];
}

CGFloat getNavHeight(void) {
    if (SCREEN_HEIGHT > 800) {
        return 88;
    } else {
        return 64;
    }
}

CGFloat getTabHeight(void) {
    if (SCREEN_HEIGHT > 800) {
        return 73;
    } else {
        return 49;
    }
}
//获取到当前所在的视图
UIViewController * getNowVC(void) {
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal){
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows){
            if (tmpWin.windowLevel == UIWindowLevelNormal){
                window = tmpWin;
                break;
            }
        }
    }
    UIViewController *result = window.rootViewController;
    while (result.presentedViewController) {
        result = result.presentedViewController;
    }
    if ([result isKindOfClass:[CYLTabBarController class]]) {
        result = [(CYLTabBarController *)result selectedViewController];
    }
    if ([result isKindOfClass:[UINavigationController class]]) {
        result = [(UINavigationController *)result topViewController];
    }
    return result;
}

NSString * getAppVersion (void) {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    // app名称
    //    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    // app版本
    NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    // app build版本
    //    NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];
    
    return app_Version;
}

NSString * getUserId(void) {
    NSString * string = [[NSUserDefaults standardUserDefaults] objectForKey:@"useronlyid"];
    if (string) {
        return string;
    } else {
        B_LoginFirstViewController * login = [[B_LoginFirstViewController alloc] init];
        UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:login];
        [getNowVC() presentViewController:nav animated:YES completion:nil];
        return nil;
    }
}


/**
 设置userID
 
 @param string useriD
 */
void setUserId(NSString * string) {
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"useronlyid"];
}

/**
 获取token
 
 @return token
 */
NSString * getToken(void) {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"user_token"];
}

/**
 设置token
 
 @param string token
 */
void setToken(NSString * string) {
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"user_token"];
}


/**
 获取手机号
 
 @return 返回手机号
 */
NSString * getMobile(void) {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"mobile"];
}


/**
 设置手机号
 
 @param string 手机号
 */
void setMobile(NSString * string) {
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"mobile"];
}

/**
 获取手机UUID

 @return 返回
 */
NSString * getDeviceNumber(void) {
    NSString * uuid = [GSKeyChainDataManager readUUID];
    if (!uuid) {
        NSString * string = [UIDevice currentDevice].identifierForVendor.UUIDString;
        [GSKeyChainDataManager saveUUID:string];
        uuid = [GSKeyChainDataManager readUUID];
    }
    return uuid;
}

/**
 获取用户的状态
 
 @return 返回用户状态
 */
NSString * getUserStatus(void) {
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"user_status"];
}

/**
 设置用户状态
 
 @param string 状态
 */
void setUserStatus(NSString * string) {
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"user_status"];
}


/**
 获取定位数据
 
 @return 返回
 */
CLLocation * getLocation(void) {
    CLLocation * location = manager.location;
    if (location) {
        return location;
    } else {
        if ([CLLocationManager locationServicesEnabled] && ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined || [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized)) {
            
            //定位功能可用
            
            if ([[NSUserDefaults standardUserDefaults] doubleForKey:@"latitude"]) {
                location = [[CLLocation alloc] initWithLatitude:[[NSUserDefaults standardUserDefaults] doubleForKey:@"latitude"] longitude:[[NSUserDefaults standardUserDefaults] doubleForKey:@"longitude"]];
            } else {
                location = [[CLLocation alloc] initWithLatitude:0 longitude:0];
                setLocation(location);
            }
            
        }else if ([CLLocationManager authorizationStatus] ==kCLAuthorizationStatusDenied) {
            location = [[CLLocation alloc] initWithLatitude:0 longitude:0];
            setLocation(location);
        }
        
        return location;
    }
}

/**
 设置定位数据
 
 @param location 定位
 */
void setLocation(CLLocation * location) {
    manager.location = location;
    [[NSUserDefaults standardUserDefaults] setDouble:location.coordinate.longitude forKey:@"longitude"];
    [[NSUserDefaults standardUserDefaults] setDouble:location.coordinate.latitude forKey:@"latitude"];
}


void setUserHasInformation(NSString * string) {
    [[NSUserDefaults standardUserDefaults] setObject:string forKey:@"userInformation"];
}

NSString * getUserHasInformation(void) {
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"userInformation"]) {
        return 0;
    }
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"userInformation"];
}




void logout (void) {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"useronlyid"];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"user_token"];
}

NS_INLINE CGFloat GETLITTLEFONT() {
    return [[ApplicationSetting getApplicationSetting][@"LittleFont"] floatValue];
}

NS_INLINE CGFloat GETMIDDLEFONT() {
    return [[ApplicationSetting getApplicationSetting][@"MiddleFont"] floatValue];
}

NS_INLINE CGFloat GETLARGEFONT() {
    return [[ApplicationSetting getApplicationSetting][@"LargeFont"] floatValue];
}

NS_INLINE CGFloat GETSCREENWIDHT() {
    return [UIScreen mainScreen].bounds.size.width;
}

@end
